package com.xuyuan.valid;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Valid;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import jakarta.validation.constraints.DecimalMax;
import jakarta.validation.constraints.DecimalMin;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.Set;

/**
 * @author xuyuan
 * @description validator校验测试
 */
public class ValidtorTest {

    @Test
    public void test0() {
        // name 加@Valid校验没有生效
        CustomObject1 customObject1 = new CustomObject1();
    }

    @Test
    public void test1() {
        // 上手测试
        CustomObject1 customObject1 = new CustomObject1();
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<CustomObject1>> violations = validator.validate(customObject1);
        // 检查是否有错误
        if (!violations.isEmpty()) {
            for (ConstraintViolation<CustomObject1> violation : violations) {
                System.out.println(violation.getMessage());
            }
        } else {
            System.out.println("验证通过");
        }
    }

    @Test
    public void test2() {
        CustomObject2 customObject2 = new CustomObject2();
        customObject2.setCustom("aaabbb");
        String message = ValidatorUtils.validatedAll(customObject2);
        System.out.println(message);
    }

    /**
     * 分组测试
     */
    @Test
    public void test3() {
        CustomObject3 customObject3 = new CustomObject3();
        customObject3.setCustom("afafasOOO");
        String message = ValidatorUtils.validatedFailFast(customObject3, CustomValidator.GroupTwo.class);
        System.out.println(message);
    }

    /**
     * 快速失败模式
     */
    @Test
    public void test4() {
        CustomObject1 customObject1 = new CustomObject1();
        String message = ValidatorUtils.validatedFailFast(customObject1);
        System.out.println(message);
    }

    @Data
    class CustomObject1 {

        @Valid
        @NotNull(message = "name can not be null") @Length(min = 2, max = 20, message = "name length must be between 2 and 20")
        @Order(1)
        String name;

        @Pattern(regexp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", message = "email format is invalid") String email;

        @Range(min = 18, max = 60, message = "age must be between 18 and 60") Integer age;

        Boolean sex;

        @DecimalMin(value = "0", message = "salary must be greater than 0") @DecimalMax(value = "100000", message = "salary must be less than 100000") BigDecimal salary;

        @Length(min = 1, max = 200, message = "address length must be between 1 and 200") String address;

        @NotNull(message = "country can not be null") String country;

        @NotNull(message = "city can not be null")
        @Order(2)
        String city;

        @NotNull(message = "zipCode can not be null") String zipCode;

        @NotNull(message = "phone can not be null") @Pattern(regexp = "^1[3-9]\\d{9}$", message = "phone format is invalid") String phone;
    }

    @Data
    class CustomObject2 {
        @CustomValidator(value = CustomValidator.CaseMode.UPPER, message = "custom必须大写")
        String custom;
    }

    @Data
    class CustomObject3 {
        @CustomValidator(value = CustomValidator.CaseMode.UPPER, message = "custom必须大写", groups = CustomValidator.GroupOne.class)
        String custom;
    }


}
